// 获取容器
let title = document.querySelector("#title");
// 渲染商品标题
function producelist() {
    let res;
    ajax({
        url: "http://chst.vip:1234/api/getcategorytitle",
        success(res) {
            res = res.result;
            let html = "";
            res.forEach(item => {
                html += `
                <i>${item.title}
                <img src="../pages/images/jiantou.png" alt="">
                </i>
                <ul id="content">
            
                </ul>`
                title.innerHTML = html;
            });
            let titlediv = title.children;
            // console.log(titlediv);
            let arr = [...titlediv];
            // console.log(arr);
            let j=0;//用于记录点击次数
            for (let i = 0; i < arr.length; i++) {
                if(i%2===0){
                    arr[i].onclick = function () {
                        j++;
                        // console.log(j);
                        if(j%2===1){
                        // console.log(this); 
                        // 获取ul
                        //   console.log(this.nextElementSibling);
                        this.nextElementSibling.style.display = "block";
                        let res;
    
                        let content = titlediv[i+1];
                        // console.log(content);
                        ajax({
                            url: "http://chst.vip:1234/api/getcategory",
                            success(res) {
                                res = res.result;
                                // console.log(res);
                                let html = "";
                                res.forEach(item => {
                                    console.log(item);
                                    html += `
                                            <a href="../pages/bijia.html?categoryid=${item.categoryId}&pageid=1">
                                            <li>${item.category}</li>
                                            </a>`
                                });
                                content.innerHTML = html;
                            },
                            data: {
                                titleid: i/2
                            },
                            error(err, xhr) {
                                console.log(err);
                            }
                        })
                        }else{
                            this.nextElementSibling.style.display = "none";
                        }
                    }
                }
            }
        }
    })
}
producelist();
// document.querySelector('#prolist').addEventListener('click', function (e) {
//     // 兼容性处理
//     var event = e || window.event;
//     var target = event.target || event.srcElement;
//     // 判断是否匹配目标元素
//    console.log(target);
//   })